let app = getApp();
const { doLogin } = require('../../../common/utils/ws/wsxcx-opendata-sdk');

Page({

  data: {
    monitorList: [],
    page: 1,
    abnorData: { imgSrc: '../../../common/static/images/abnor-02.png', text: '还没有相关监控数据！' },
    abnorD: false,
    endText: { isEnd: true, icon: '', text: '我是有底线的' },
  },

  onLoad: function(options) {
    let that = this;
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 15000,
      mask: true,
    });

    doLogin().then(res => {
      wx.setStorageSync('userInfo', { sessionid: res.sessionID, openid: res.openID });
      setTimeout(function() {
              that.getVideoList();
      }, 540);
          });
  },

  getVideoList() {
    let that = this;
    let sessionid = wx.getStorageSync('userInfo') && wx.getStorageSync('userInfo').sessionid || '';
    let {
      siteroot,
      uniacid,
      multiid,
      version,
    } = app.siteInfo;
    wx.request({
      url: siteroot + '?i=' + uniacid + '&t=' + multiid + '&v=' + version + '&from=wxapp&m=cm_monitor&c=entry&a=wxapp&do=index&state=v8uuid-' + sessionid,
      data: {
        op: 'getCameraList',
        page: that.data.page,
        psize: 10,
      },
      success: (res) => {
        let { data } = res.data;
        data.length > 0 && that.setData({ monitorList: that.data.monitorList.concat(data) });
        that.setData({ abnorD: that.data.monitorList.length > 0 ? false : true, data: data.length > 0 ? false : true });
        wx.stopPullDownRefresh();
        wx.hideToast();
      },
    });
  },

  onPullDownRefresh() {
    this.setData({ page: 1, monitorList: [], data: false });
    this.getVideoList();
  },

  onReachBottom() {
    let that = this;
    if (that.data.data) {
      this.setData({ page: that.data.page + 1 });
      this.getVideoList();
    }
  },

  goDetail(e) {
    let { vid, vurl } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../detail/detail?id=' + vid + '&vurl=' + vurl,
    });
  },
});